package LeetCode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Lc145 {
    public static void main(String[] args) {

    }
    public List<Integer> postorderTraversal(TreeNode root) {
        ArrayList<Integer> ans = new ArrayList<>();
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();
        if(root==null) return ans;
        stack1.push(root);
        while (!stack1.isEmpty()){
            TreeNode pop = stack1.pop();
            stack2.add(pop);
            if(pop.left!=null){
                stack1.push(pop.left);
            }
            if(pop.right!=null){
                stack1.push(pop.right);
            }
        }
        while (!stack2.isEmpty()){
            ans.add(stack2.pop().val);
        }
        return ans;
    }
}
